import { ApiRes, SearchResult, Suggestion } from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "@/utils/http";
// 获得搜索建议
export const getSuggestion = (value: string): RootThunkAction => {
    return async (dispatch) => {
        // 清空搜索建议栏
        if (!value) {
            return dispatch({ type: "search/suggestion", payload: [] });
        }
        const res = await http.get<ApiRes<Suggestion>>("/suggestion", {
            params: {
                q: value,
            },
        });
        dispatch({
            type: "search/suggestion",
            payload: res.data.data.options,
        });
    };
};
// 获得搜索结果
export const getSearchResult = (query: string): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.get<ApiRes<SearchResult>>("/search", {
            params: {
                q: query,
                page: 1,
            },
        });

        dispatch({ type: "search/getSearchResult", payload: res.data.data });
    };
};
